package test;

import java.util.ArrayList;
import java.util.Scanner;

public class Student1Manger {
    public static void main(String[] args) {
        ArrayList<Student1> array = new ArrayList<Student1>();

        while (true) {
            System.out.println("-------欢迎进入学生管理系统--------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看所有学生");
            System.out.println("5.退出");
            System.out.println("请按照号码输入您需要的操作");
            System.out.println();

            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();

            switch (line) {
                case "1":
//                    System.out.println("添加学生");
                    addStudent1(array);
                    break;
                case "2":
//                    System.out.println("删除学生");
                    deleteStudent1(array);
                    break;

                case "3":
                    System.out.println("修改学生");
                    updataStudent1(array);
                    break;
                case "4":
                    System.out.println("查看所有学生");
                    findallStudent(array);
                    break;
                case "5":
                    System.out.println("感谢使用");
                    System.exit(0);
            }
        }


    }


    public static void addStudent1(ArrayList<Student1> array) {

        Scanner sc = new Scanner(System.in);
        String sid;

        while (true) {
            System.out.println("请输入学生的学号");
            sid = sc.nextLine();

            boolean flag = isused(array, sid);
            if (flag) {
                System.out.println("您输入的信息有误，请重新输入");
            } else {
                break;
            }
        }

            System.out.println("请输入学生的姓名");
            String name = sc.nextLine();

            System.out.println("请输入学生的年龄");
            String age = sc.nextLine();

            System.out.println("请输入学生的地址");
            String address = sc.nextLine();

            Student1 s = new Student1();
            s.setSid(sid);
            s.setName(name);
            s.setAge(age);
            s.setAddress(address);
            array.add(s);
            System.out.println("添加学生成功");
        }



    public static void findallStudent(ArrayList<Student1> array) {
        if(array.size()==0){
            System.out.println("信息有误，请重新输入");
            return ;
        }
        System.out.println("学号\t姓名\t年龄\t地址");
        for (int i = 0; i < array.size(); i++) {
            Student1 s = array.get(i);
            System.out.println(s.getSid() +"\t"+ s.getName()+"\t" + s.getAge() + "岁\t" + s.getAddress());

    }

}
        public static void deleteStudent1(ArrayList<Student1>array){
        Scanner sc=new Scanner(System.in);
            System.out.println("请输入要删除学生的学号");
        String sid=sc.nextLine();

        int index = -1;

            for (int i = 0; i < array.size() ; i++) {
                Student1 stu = array.get(i);
                if (stu.getSid().equals(sid)) {
                    index = i;
                    break;
                }
            }
                if(index==-1){
                    System.out.println("您输入的有误，请重新输入");
                }else{
                    array.remove(index);
                    System.out.println("学生删除成功");
                }


            }

        public static String updataStudent1(ArrayList<Student1>array){
        Scanner sc=new Scanner(System.in);
            System.out.println("请输入要修改的学生的学号");
            String sid=sc.nextLine();
            System.out.println("请输入修改的名字");
            String name=sc.nextLine();
            System.out.println("请输入修改的年龄");
            String age=sc.nextLine();
            System.out.println("请输入修改的地址");
            String address=sc.nextLine();

                Student1 s=new Student1();
                    s.setName(name);
                    s.setAge(age);
                    s.setAddress(address);

                int index=-1;
            for (int i = 0; i < array.size(); i++) {
                Student1 student=array.get(i);
                if (student.equals(sid)){
                    index=i;
                    break;
            }
            }
            if(index==-1){
                System.out.println("您输入的信息有误请重新输入");
            }else {
                array.set(index,s);
                System.out.println("修改信息成功");
            }


            return sid;
        }
        public static  boolean isused(ArrayList<Student1>array, String sid){
        boolean flag=false;
            for (int i = 0; i < array.size() ; i++) {
                Student1 s=array.get(i);
                if(array.get(i).equals(sid)){
                    flag= true;
                    break;
                }
            }
            return  flag;
    }
    }

